import { createRouter, createWebHashHistory } from "vue-router";
import HomeView from "../views/HomeView.vue";
const routes = [
  {
    path: "/home",
    name: "home",
    component: HomeView,
    children: [
      // 主页
      {
        path: "index",
        component: () => import("../views/index/Index.vue"),
      },
      // 圈子
      {
        path: "circle",
        component: () => import("../views/circle/Circle.vue"),
      },
      // 我的
      {
        path: "me",
        component: () => import("../views/me/Me.vue"),
      },
      {
        path: "shopping",
        component: () => import("../views/shopping/Shoppng.vue"),
      },
    ],
  },
  {
    path: "/",
    redirect: "/home/index",
  },
  //帖子详情
  {
    path: "/circle-comment/:id/:name/:u_id/:u_img",
    name: "circle-comment",
    component: () => import("../views/circle/CircleComment.vue"),
  },
  //发布帖子
  {
    path: "/publish/:id",
    name: "publish",
    component: () => import("../views/circle/publishs.vue"),
  },

  // 登录
  {
    path: "/login",
    name: "login",
    component: () => import("../views/login/Login.vue"),
  },
  // 注册
  {
    path: "/signin",
    name: "signin",
    component: () => import("../views/login/SignIn.vue"),
  },
  // 主页下大分类跳转到小分类
  {
    path: "/small-class/:v_id",
    name: "small-class",
    component: () => import("../views/index/SmallClass.vue"),
  },
  // 宠物详情
  {
    path: "/petinformation/:v_id/:p_id",
    component: () => import("../views/index/PetInformation.vue"),
  },
  // 商品详情
  {
    path: "/shopDetail",
    component: () => import("../views/shopping/shopDetail.vue"),
  },
  //个人设置
  {
    path: '/set/:id',
    component: () => import('../views/me/set.vue')
  },
  //编辑资料
  {
    path: '/edit/:id',
    component: () => import('../views/me/edit.vue')
  },
  //修改头像
  {
    path: '/head/:id',
    component: () => import('../views/me/head.vue')
  },
  //密码
  {
    path: '/updatepwd/:id',
    component: () => import('../views/me/updatepwd.vue')
  },
];

const router = createRouter({
  history: createWebHashHistory(process.env.BASE_URL),
  routes,
});

// 添加全局路由守卫
// 注册全局的路由前置守卫

// GOOD
router.beforeEach((to, from, next) => {
  if (to.path == '/login' || to.path == '/signin') {
    next()
  } else {
    if (localStorage.getItem('user') && localStorage.getItem('token')) {
      next()
    } else {
      router.push('/login')
    }
  }
})


export default router;
